package com.sanmina.route.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sanmina.route.dao.RouteDao;
import com.sanmina.route.dao.RouteDaoImpl;
import com.sanmina.route.entity.RouteStep;

import net.sf.json.JSONObject;

@WebServlet("/saveRouteStep")
public class SaveRouteStepAction extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1013210403298682830L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String stepsData = req.getParameter("stepsData");
		String routeName = req.getParameter("routeName");
		System.out.println(routeName + ":  " + stepsData);

		RouteDao dao = new RouteDaoImpl();
		
		int routeKey = dao.isRouteExist(routeName).getRouteKey();
		
		dao.deleteRouteStepByRouteKey(routeKey);
		
		String everyStep[] = stepsData.split(";");
		for (int i = 0; i < everyStep.length; i++) {
			String stepkeys[] = everyStep[i].split(",");

			int step = Integer.valueOf(stepkeys[0]);
			int fail = 0;
			int loc = 0;
			int pass = 0;
			int from1 = 0;
			int from2 = 0;
			int from3 = 0;
			int from4 = 0;
			int insertedRoute = 0;
			int createUnit = 0;
			if (step == 10) {
				createUnit = 1;
			}

			if ((i + 1) != everyStep.length) {
				String nextLoc[] = everyStep[i + 1].split(",");
				for (int j = 1; j < nextLoc.length; j++) {
					String passLoc = nextLoc[j].substring(0, 3);
					if (passLoc.equals("rou") || passLoc.equals("gre")) {
						pass = Integer.valueOf(nextLoc[j].substring(5, nextLoc[j].length()));
					}
				}
			}

			for (int j = 1; j < stepkeys.length; j++) {

				if (stepkeys[j].substring(0, 3).equals("red")) {
					fail = Integer.valueOf(stepkeys[j].substring(3, stepkeys[j].length()));
				} else if (stepkeys[j].substring(0, 3).equals("gre")) {
					loc = Integer.valueOf(stepkeys[j].substring(5, stepkeys[j].length()));
				} else if (stepkeys[j].substring(0, 3).equals("rou")) {
					loc = Integer.valueOf(stepkeys[j].substring(5, stepkeys[j].length()));
					insertedRoute = 1;

				} else if (stepkeys[j].substring(0, 3).equals("blu")) {
					if (from1 == 0) {
						from1 = Integer.valueOf(stepkeys[j].substring(4, stepkeys[j].length()));
					} else if (from2 == 0) {
						from2 = Integer.valueOf(stepkeys[j].substring(4, stepkeys[j].length()));
					} else if (from3 == 0) {
						from3 = Integer.valueOf(stepkeys[j].substring(4, stepkeys[j].length()));
					} else if (from4 == 0) {
						from4 = Integer.valueOf(stepkeys[j].substring(4, stepkeys[j].length()));
					}
				}
			}

			if (fail != 0) {
				RouteStep failStep = new RouteStep(routeKey, step + 5, fail, 0, loc, 0, fail, 0, 0, 0, 0);
				dao.addRouteStep(failStep);
			}

			if ((i + 1) == everyStep.length) {
				dao.updateRouteStepsNumber(routeKey, step / 10);
			}

			System.out.println(
					step + ":\n fail: " + fail + ",Loction: " + loc + ",pass: " + pass + ",from1:" + from1 + ",from2:"
							+ from2 + ",from3:" + from3 + ",from4:" + from4 + "  " + insertedRoute + " " + createUnit);
			RouteStep routeStep = new RouteStep(routeKey, step, loc, insertedRoute, pass, fail, from1, from2, from3,
					from4, createUnit);
			dao.addRouteStep(routeStep);
		}
		JSONObject jo = new JSONObject();
		jo.put("routeKey", routeKey);
		resp.setCharacterEncoding("UTF-8");
		resp.setHeader("Cache-Control", "no-cache");
		resp.setContentType("text/html;charset=UTF-8");
		resp.getWriter().print(jo);
		resp.getWriter().close();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doGet(req, resp);
	}

}
